package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

import static com.sky.constant.StatusConstant.ENABLE;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void save(SetmealDTO dto) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmeal.setStatus(1);
//        先把套餐插入表中
        setMealMapper.insert(setmeal);
//        还要把套餐关联的菜品都插入表中
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
//   别忘了把setmealId赋值了
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishes.forEach(System.out::println);
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        page.forEach(setmeal -> {
            System.out.println(setmeal.getCategoryId());
        });
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public SetmealDTO getById(Long id) {
//        由于套餐关联了很多菜品,所以得分两步查询
//        第一步,查出当前id的套餐
//        第二步,根据套餐id,去setmealdish表里查当前套餐关联的菜品数据
        Setmeal setmeal = setMealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);
        setmealDTO.setSetmealDishes(setmealDishes);
        System.out.println(setmealDTO.getSetmealDishes());
        return setmealDTO;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal=setMealMapper.selectById(id);
        setmeal.setStatus(status);
        setMealMapper.update(setmeal);
    }

    @Override
    public void update(SetmealDTO dto) {
//        这里涉及到了两张表,为了方便
//        直接把这个套餐关联的所有菜品都删除，然后再插入新编辑的菜品
        setmealDishMapper.deleteBySetmealId(dto.getId());
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(dto.getId()));
        setmealDishMapper.insertBatch(setmealDishes);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setMealMapper.update(setmeal);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
//        先检查一下有没有正在出售的套餐,如果有,就要报异常
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.selectById(id);
            if (Objects.equals(setmeal.getStatus(), ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
//       先把每个套餐关联的菜品都删掉
//        再把这个套餐删掉
        for(Long id:ids){
            setmealDishMapper.deleteBySetmealId(id);
            setMealMapper.deleteById(id);
        }
    }

    @Override
    public List<SetmealDish> getDishBySetmealId(Long id) {
       return setmealDishMapper.getBySetmealId(id);
    }

    @Override
    public List<Setmeal> list(Long categoryId) {
        return setMealMapper.list(categoryId);
    }
}
